package cn.kgc.service;

import cn.kgc.common.CrowdFundingUtils;
import cn.kgc.mapper.BuildingMapper;
import cn.kgc.pojo.Building;
import cn.kgc.pojo.BuildingExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-03-16 9:18
 */
@Service("buildingService")
public class BuildingServiceImpl implements  BuildingService{

    @Resource
    BuildingMapper buildingMapper;

    @Override
    public PageInfo<Building> queryForKeywordSearchBuilding(Integer pageNum, Integer pageSize, String keyword) {

        PageHelper.startPage(pageNum,pageSize);

        if(CrowdFundingUtils.stringEffective(keyword)){
            BuildingExample example=new  BuildingExample();
            BuildingExample.Criteria criteria = example.createCriteria();
            criteria.andBuildingnameLike("%"+keyword+"%");
            List<Building> buildings = buildingMapper.selectByExample(example);
            PageInfo<Building> pageInfo=new PageInfo<>(buildings);
            return pageInfo;
        }else{
            BuildingExample example=new  BuildingExample();
            BuildingExample.Criteria criteria = example.createCriteria();
            List<Building> buildings = buildingMapper.selectByExample(null);
            PageInfo<Building> pageInfo=new PageInfo<>(buildings);
            return pageInfo;
        }
    }

    @Override
    public void insertBuilding(Building building) {
        buildingMapper.insert(building);
    }

    @Override
    public Building selectByIdBuilding(Integer id) {
    return    buildingMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateBuilding(Building building) {
        buildingMapper.updateByPrimaryKeySelective(building);
    }

    @Override
    public void deleteBuilding(List<Integer> id) {
        BuildingExample example=new BuildingExample();
        BuildingExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(id);
        buildingMapper.deleteByExample(example);
    }

    @Override
    public void removedeleteBuilding(Integer id) {
        buildingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Building selectByIdBuildingXiang(Integer id) {
        return buildingMapper.selectByPrimaryKey(id);
    }
}
